package com.wufeng.model.SpringBootModel.other;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class JSONObjectDemo {
    public static void main(String[] args) {
        String requestJson = "{\"lisi\":\"20\",\"zhangsan\":\"24\"}";
        HashMap<String, String> addMap = (HashMap<String, String>) JSONObject.parseObject(requestJson, HashMap.class);
        String jsonString = "[{\"doc_count\":80,\"key\":\"nnnnnnn\"},{\"doc_count\":70,\"key\":\"汤姆·布伦德尔\"},{\"doc_count\":90,\"key\":\"李国强\"},{\"doc_count\":87,\"key\":\"徐明非\"}]";
        JSONArray jsonArray = JSONArray.parseArray(jsonString);
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            map.put(jsonArray.getJSONObject(i).getString("key"),i);
        }
        System.out.println(jsonArray.toString());
        addMap.keySet().stream().forEach(item -> {
            if (map.containsKey(item)) {
                int index = map.get(item);
                jsonArray.getJSONObject(index).remove("doc_count");
                jsonArray.getJSONObject(index).put("doc_count",addMap.get(item));
            }else {
                JSONObject newJson = new JSONObject();
                newJson.put("key",item);
                newJson.put("doc_count",addMap.get(item));
                jsonArray.add(newJson);
            }
        });
        System.out.println(jsonArray.toString());
    }
}
